//
//  HSYCustomArcView.m
//  CustomNavigationController
//
//  Created by key on 2017/2/22.
//  Copyright © 2017年 huangsongyao. All rights reserved.
//

#import "HSYCustomArcView.h"
#import "HSYCustomArcView+Animation.h"
#import "UIView+Bezier.h"
#import <Masonry.h>
#import "SchoLineView.h"

#define DEFAULT_ROUND_OFFSET_X                  1.0f
#define DEFAULT_ROUND_OFFSET_Y                  DEFAULT_ROUND_OFFSET_X

#define DEFAULT_LINE_WIDTHS                     0.32f
#define DEFAULT_LINE_HEIGHT                     1.0f
#define DEFAULT_LINE_OFFSET_TOP                 14.0f

#define LEVEL_OFFSET_TOP                        5.0f

@interface HSYCustomArcView ()

@property (nonatomic, strong) UILabel *progressLabel;
@property (nonatomic, strong) UILabel *levelLabel;

@end

@implementation HSYCustomArcView

- (instancetype)initWithFrame:(CGRect)frame roundFillColor:(UIColor *)fillColor roundStrokeColor:(UIColor *)strokeColor roundLineWidth:(CGFloat)width
{
    if (self = [super initWithFrame:frame]) {
        
        //画一个实心圆作为外层
        [self bezierForOvalInRect:self.bounds roundColor:fillColor];
        //画一个空心圆作为内存
        [self bezierForOvalInRect:CGRectMake(DEFAULT_ROUND_OFFSET_X+width/2, DEFAULT_ROUND_OFFSET_Y+width/2, self.bounds.size.width - DEFAULT_ROUND_OFFSET_X*2-width, self.bounds.size.height - DEFAULT_ROUND_OFFSET_Y*2-width) roundStrokeColor:strokeColor roundLineWidth:width];
        
        //添加定制的动画layer层最为最内层
        self.arcLayer = [HSYCustomArcLayer layer];
        self.arcLayer.frame = self.bounds;
        self.arcLayer.contentsScale = [UIScreen mainScreen].scale;
        [self.layer addSublayer:self.arcLayer];
        
        SchoLineView *line = [[SchoLineView alloc] initWithSize:CGSizeMake(self.width *DEFAULT_LINE_WIDTHS, DEFAULT_LINE_HEIGHT) color:[UIColor whiteColor]];
        line.frame = CGRectMake((self.width - self.width *DEFAULT_LINE_WIDTHS)/2, self.center.y + DEFAULT_LINE_OFFSET_TOP, self.width *DEFAULT_LINE_WIDTHS, DEFAULT_LINE_HEIGHT);
        [self addSubview:line];
    }
    return self;
}

- (void)setProgress:(CGFloat)progress
{
    if (progress <= 0 || progress > 1.0f) {
        return;
    }
    [self addArcAnimationToValue:progress];
    _progress = progress;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

@implementation HSYCustomArcView (Show)

+ (HSYCustomArcView *)showArcViewByPropertyParams:(NSDictionary <NSNumber *, id>*)params frame:(CGRect)frame showInView:(UIView *)view
{
    UIColor *fillColor = params[@(kCustomArcViewForPropertyTypeFillColor)];
    UIColor *strokeColor = params[@(kCustomArcViewForPropertyTypeStrokeColor)];
    CGFloat lineWidth = [params[@(kCustomArcViewForPropertyTypeLineWidth)] floatValue];
    HSYCustomArcView *arcView = [[HSYCustomArcView alloc] initWithFrame:frame roundFillColor:fillColor roundStrokeColor:strokeColor roundLineWidth:lineWidth];
    
    [view addSubview:arcView];
    return arcView;
}

@end
